﻿using System;
using System.Runtime.InteropServices;

namespace OdsServer.Device.Core
{
    internal static unsafe class DeviceDriverAdv
    {
        // General calls - 100% tested
        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_SelectDevice(
                IntPtr hCaller,
                bool GetModule,
                IntPtr DeviceNum,
                IntPtr Description);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_DeviceGetList(
                IntPtr DeviceList,
                short MaxEntries,
                IntPtr OutEntries);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_DeviceOpen(
                Int32 DeviceNum,
                IntPtr DriverHandle);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_DeviceClose(
                IntPtr DriverHandle);

        //Analog Input Function Group
        //Software Transferring
        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_AIConfig(
                IntPtr DriverHandle,
                IntPtr lpAIConfig);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_AIVoltageIn(
                IntPtr DriverHandle,
                IntPtr lpAIVoltageIn);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_AIGetConfig(
                IntPtr DriverHandle,
                IntPtr lpAIGetConfig);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_AIBinaryIn(
                IntPtr DriverHandle,
                IntPtr lpAIBinaryIn);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_AIScale(
                IntPtr DriverHandle,
                IntPtr lpAIScale);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_AIVoltageInExp(
                IntPtr DriverHandle,
                IntPtr lpAIVoltageInExp);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_MAIConfig(
                IntPtr DriverHandle,
                IntPtr lpMAIConfig);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_MAIVoltageIn(
                IntPtr DriverHandle,
                IntPtr lpMAIVoltageIn);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_MAIBinaryIn(
                IntPtr DriverHandle,
                IntPtr lpMAIBinaryIn);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_MAIVoltageInExp(
                IntPtr DriverHandle,
                IntPtr lpMAIVoltageInExp);

        //Analog Input Function Group
        //Fast AI Transferring
        //Interrupt Transferring
        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_FAIIntStart(
                IntPtr DriverHandle,
                IntPtr lpFAIIntStart);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_FAIIntScanStart(
                IntPtr DriverHandle,
                IntPtr lpFAIIntScanStart);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_FAITerminate(
                IntPtr DriverHandle);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_FAITransfer(
                IntPtr DriverHandle,
                IntPtr lpFAITransfer);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_FAICheck(
                IntPtr DriverHandle,
                IntPtr lpFAICheck);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_ClearOverrun(
                IntPtr DriverHandle);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_GetFIFOSize(
                IntPtr DriverHandle,
                IntPtr lSize);

        //Analog Input Function Group
        //Fast AI Transferring
        //DMA Transferring
        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_AllocateDMABuffer(
                IntPtr DriverHandle,
                IntPtr lpAllocateDMABuffer);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_FreeDMABuffer(
                IntPtr DriverHandle,
                IntPtr buffer);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_FAIDmaStart(
                IntPtr DriverHandle,
                IntPtr lpFAIDmaStart);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_FAIDMAScanStart(
                IntPtr DriverHandle,
                IntPtr lpFAIDmaScanStart);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_FAIDmaExStart(
                IntPtr DriverHandle,
                IntPtr lpFAIDmaExStart);

        //Analog Output Function Group
        //Software Transferring
        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_AOConfig(
                IntPtr DriverHandle,
                IntPtr lpAOConfig);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_AOVoltageOut(
                IntPtr DriverHandle,
                IntPtr lpAOVoltageOut);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_AOCurrentOut(
                IntPtr DriverHandle,
                IntPtr lpAOCurrentOut);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_AOBinaryOut(
                IntPtr DriverHandle,
                IntPtr lpAOBinaryOut);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_AOScale(
                IntPtr DriverHandle,
                IntPtr lpAOScale);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_EnableSyncAO(
                IntPtr DriverHandle,
            /* check type */IntPtr Enable);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_WriteSyncAO(
                IntPtr DriverHandle);

        //Analog Output Function Group
        //Fast AO Transferring
        //DMA Transferring
        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_FAODmaStart(
                IntPtr DriverHandle,
                IntPtr lpFAODmaStart);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_FAOWaveFormStart(
                IntPtr DriverHandle,
                IntPtr lpFAOWaveFormStart);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_FAOLoad(
                IntPtr DriverHandle,
                IntPtr lpFAOLoad);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_FAOCheck(
                IntPtr DriverHandle,
                IntPtr lpFAOCheck);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_FAOScale(
                IntPtr DriverHandle,
                IntPtr lpFAOScale);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_FAOTerminate(
                IntPtr DriverHandle);

        //Digital Input
        //Software Transferring
        [DllImport("Adsapi32.dll")]
        public static extern Int32 AdxDioReadDiPorts(
                IntPtr DriverHandle,
                UInt32 dwPortStart,
                UInt32 dwPortCount,
                IntPtr pBuffer);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_DioGetConfig(
                IntPtr DriverHandle,
                IntPtr lpDioGetConfig);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_DioSetPortMode(
                IntPtr DriverHandle,
                IntPtr lpDioSetPortMode);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_DioReadPortByte(
                IntPtr DriverHandle,
                IntPtr lpDioReadPortByte);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_DioReadBit(
                IntPtr DriverHandle,
                IntPtr lpDioReadBit);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_DioReadPortWord(
                IntPtr DriverHandle,
                IntPtr lpDioReadPortWord);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_DioReadPortDword(
                IntPtr DriverHandle,
                IntPtr lpDioReadPortDword);

        //Digital Input
        //Interrupt Transferring
        [DllImport("Adsapi32.dll")]
        public static extern Int32 AdxDioDisableEvent(
                IntPtr DriverHandle,
                UInt32 dwEventID);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 AdxDioGetLatestEventDiPortsState(
                IntPtr DriverHandle,
                UInt32 dwEventID,
                IntPtr pBuffer,
                UInt32 dwLength);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 AdxDioEnableEventAndSpecifyDiPorts(
                IntPtr DriverHandle,
                UInt32 dwEventID,
                UInt32 dwScanStart,
                UInt32 dwScanCount);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_FDITransfer(
                IntPtr DriverHandle,
                IntPtr lpFDITransfer);

        //Digital Input
        //DMA Transferring
        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_FDIStart(
                IntPtr DriverHandle,
                UInt16 wCyclic,
                UInt32 dwCount,
                IntPtr pBuf);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_FDIStop(
                IntPtr DriverHandle);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_FDICheck(
                IntPtr DriverHandle,
                IntPtr pdwStatus,
                IntPtr pdwRetrieved);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_ClearFlag(
                IntPtr DriverHandle,
                UInt32 dwEventType);

        //Digital Output
        //Software Transferring
        [DllImport("Adsapi32.dll")]
        public static extern Int32 AdxDioWriteDoPorts(
                IntPtr DriverHandle,
                UInt32 dwPortStart,
                UInt32 dwPortCount,
                IntPtr pBuffer);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 AdxDioGetCurrentDoPortsState(
                IntPtr DriverHandle,
                UInt32 dwPortStart,
                UInt32 dwPortCount,
                IntPtr pBuffer);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_DioWriteBit(
                IntPtr DriverHandle,
                IntPtr lpDioWriteBit);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_DioWritePortByte(
                IntPtr DriverHandle,
                IntPtr lpDioWritePortByte);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_DioWritePortWord(
                IntPtr DriverHandle,
                IntPtr lpDioWritePortWord);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_DioWritePortDword(
                IntPtr DriverHandle,
                IntPtr lpDioWritePortDword);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_DioGetCurrentDOBit(
                IntPtr DriverHandle,
                IntPtr lpDioGetCurrentDOBit);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_DioGetCurrentDOByte(
                IntPtr DriverHandle,
                IntPtr lpDioGetCurrentDOByte);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_DioGetCurrentDOWord(
                IntPtr DriverHandle,
                IntPtr lpDioGetCurrentDOWord);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_DioGetCurrentDODword(
                IntPtr DriverHandle,
                IntPtr lpDioGetCurrentDODword);

        //Digital Output
        //DMA Transferring
        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_FDOStart(
                IntPtr DriverHandle,
                UInt16 wCyclic,
                UInt32 dwCount,
                IntPtr pBuf);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_FDOCheck(
                IntPtr DriverHandle,
                IntPtr pdwStatus,
                IntPtr pdwRetrieved);

        [DllImport("Adsapi32.dll")]
        public static extern Int32 DRV_FDOStop(
                IntPtr DriverHandle);
    }
}
